package com.gugee.tiktok.common.model.es.music;

import com.gugee.tiktok.common.model.mysql.Music;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.StringUtils;

import java.util.List;

@Data
public class ESMusic {
    private Long id;
    private Long mid;
    private String author;
    private String title;
    private Boolean authorDeleted;
    private Integer duration;
    private Long uid;
    private Long userGrowth;
    private Long userCount;
    private Boolean isOriginal;
    private String offlineDesc;
    private String coverMedium;
    private String region;
    private String playUrl;
    private String shareUrl;
    private String qrCode;
    private List<ESVideo> topVideos;
    private List<ESDayVideoGrowth> videoGrowths;
    private List<ESCategory> categories;

    @Mapper(componentModel = "spring")
    public interface ESMusicMapping{

        @Mapping(target = "topVideos", expression = "java(setTopVideos(music))")
        @Mapping(target = "videoGrowths", expression = "java(setVideoGrowths(music))")
        ESMusic mappger(Music music);

        List<ESMusic> mappger(List<Music> music);


        default List<ESVideo> setTopVideos(Music music){
            if(!StringUtils.isEmpty(music.getTopVideos())){
                return JsonUtil.jsonParse(List.class,music.getTopVideos());
            }
            return null;
        }

        default List<ESDayVideoGrowth> setVideoGrowths(Music music){
            if(!StringUtils.isEmpty(music.getDayVideoGrowth())){
                return JsonUtil.jsonParse(List.class,music.getDayVideoGrowth());
            }
            return null;
        }
    }
}
